package service.book;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import dao.book.BookDao;
import dao.book.BookDaoImpl;
import entity.Book;
import utils.SqlConnection;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class BookServerImpl implements BookServer {
	Connection connection;
	BookDao bookDao = new BookDaoImpl();

//	Book book = null;

	@Override
	public boolean addBook(Book book) {
//		 TODO Auto-generated method stub
		connection = SqlConnection.getConnection();
		int row = 0;
		try {
			row = bookDao.addBook(connection, book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return row > 0;
	}

	@Override
	public List<Book> getBookList(Book book) {
		// TODO Auto-generated method stub
		if (book == null)
			book = new Book();
		connection = SqlConnection.getConnection();
		ArrayList<Book> bookList = new ArrayList<Book>();
		try {
			bookList = (ArrayList<Book>) bookDao.getBookList(connection, book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bookList;
	}

	@Override
	public boolean upBook(Book book) {
		// TODO Auto-generated method stub
		if (book == null)
			book = new Book();
		connection = SqlConnection.getConnection();
		int row = 0;
		try {
			row = bookDao.upBook(connection, book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row > 0;
	}

	@Override
	public boolean deleteBook(int id) {
		// TODO Auto-generated method stub
		connection = SqlConnection.getConnection();

		int row = 0;

		try {
			row = bookDao.delBook(connection, id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return row > 0;
	}

}
